package hasthi.manager.gui;

import hasthi.actuators.ActionContext;
import hasthi.actuators.SystemActionsScheduler;
import hasthi.common.BaseContext;
import hasthi.common.ComponetFactory;
import hasthi.common.HasthiException;
import hasthi.common.HasthiOptions;
import hasthi.common.SafeScheduler;
import hasthi.container.UnicastContianier;
import hasthi.manager.ManagementRingDocument.ManagementRing.ManagerData;
import hasthi.metamodel.ExternalResourceModel;
import hasthi.metamodel.ResourceModelFactory;
import hasthi.metamodel.resource.ResourceModelContext;

import java.util.List;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JTextArea;

public class UIContext {
    private UnicastContianier container;
    private BaseContext  baseContext;
    private ExternalResourceModel externalResourceModel;
    private List<ManagerData> managers = new Vector<ManagerData>();
    private ResourceModelContext resourceModelContext;
    private SystemActionsScheduler actionScheduler;
    private OutputConsole outputConsole = new OutputConsole();     
    public ResourceModelContext getResourceModelContext() {
        return resourceModelContext;
    }
    public void setResourceModelContext(ResourceModelContext resourceModelContext) {
        this.resourceModelContext = resourceModelContext;
    }
    
    public UIContext(boolean serach4Ring) throws HasthiException {
        this(serach4Ring,(String)null);
    }
    
//    public UIContext(boolean serach4Ring,BaseContext baseContext) throws HasthiException {
    public UIContext(boolean serach4Ring,String bootstrapNode) throws HasthiException {
        if(bootstrapNode != null){
            Properties properties = new Properties();
            properties.setProperty(HasthiOptions.BOOTSTRAP_NODE, bootstrapNode);
            baseContext = new BaseContext(properties,true);
        }else{
            baseContext = new BaseContext(null);    
        }
        container = ComponetFactory.createSoapContianier(baseContext,null);
        if(serach4Ring){
            externalResourceModel = ResourceModelFactory.getExternalResourceModel(this);
        }
        actionScheduler = new SystemActionsScheduler();
        ActionContext actionContext = new ActionContext(baseContext, false);
        actionContext.setScheduler(new SafeScheduler(2,new SafeScheduler.JobCancelledCallback() {
            public void jobCancelled(Runnable runnable) {
                System.out.println("Canceled");
            }
        
        },true));
        actionContext.setResourceModel(externalResourceModel);
        actionContext.setUnicastContainer(ComponetFactory.createSoapContianier(baseContext, null));

        actionScheduler.init(actionContext);
    }  
        
   public UIContext(boolean serach4Ring,BaseContext baseContext) throws HasthiException {
       this.baseContext = baseContext;
        container = ComponetFactory.createSoapContianier(baseContext,null);
        if(serach4Ring){
            externalResourceModel = ResourceModelFactory.getExternalResourceModel(this);
        }
        actionScheduler = new SystemActionsScheduler();
        ActionContext actionContext = new ActionContext(baseContext, false);
        actionContext.setScheduler(new SafeScheduler(2,new SafeScheduler.JobCancelledCallback() {
            public void jobCancelled(Runnable runnable) {
                System.out.println("Canceled");
            }
        
        },true));
        actionContext.setResourceModel(externalResourceModel);
        actionContext.setUnicastContainer(ComponetFactory.createSoapContianier(baseContext, null));

        actionScheduler.init(actionContext);
    }
    
    public String getCoordinatorUrl() {
        if(resourceModelContext != null){
            return resourceModelContext.getCoordinatorUrl();
        }
        return null;
    }
    public UnicastContianier getContainer() {
        return container;
    }
    public BaseContext getBaseContext() {
        return baseContext;
    }
    public ExternalResourceModel getExternalResourceModel() {
        return externalResourceModel;
    }
    public List<ManagerData> getManagers() {
        return managers;
    }
    public void setExternalResourceModel(ExternalResourceModel externalResourceModel) {
        this.externalResourceModel = externalResourceModel;
        if(actionScheduler != null){
            actionScheduler.getActionContext().setResourceModel(externalResourceModel);
        }
    }
    public SystemActionsScheduler getActionScheduler() {
        return actionScheduler;
    }
    public OutputConsole getOutputConsole() {
        return outputConsole;
    }
    
   
    
}
